package com.example.userp.app.myapplication.InnerPrient;

import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.sunmi.peripheral.printer.InnerPrinterCallback;
import com.sunmi.peripheral.printer.InnerPrinterException;
import com.sunmi.peripheral.printer.InnerPrinterManager;
import com.sunmi.peripheral.printer.InnerResultCallbcak;
import com.sunmi.peripheral.printer.SunmiPrinterService;

/**
 * Describe:
 * Created by ysy on 2019/1/29.
 */
public class InnerPrint {

    /**
     * 初始化
     * @param context
     * @throws InnerPrinterException
     */
    public InnerPrint(Context context) throws InnerPrinterException {

//        innerPrinterCallback即服务绑定回调
        InnerPrinterCallback innerPrinterCallback = new InnerPrinterCallback() {
            @Override
            protected void onConnected(SunmiPrinterService service) {
                Log.e("tag", "打印启动 onConnected: " );
                PrintString(service);
            }

            @Override
            protected void onDisconnected() {
                //当服务异常断开后，会回调此⽅法 } }
                Log.e("tag", "服务异常 onDisconnected: " );
            }
        };
        InnerPrinterManager.getInstance().bindService(context, innerPrinterCallback);
    }

    /**
     * 打印
     * @param sunmiPrinterService
     */
    private void PrintString(SunmiPrinterService sunmiPrinterService) {
        try {
            sunmiPrinterService.printText("要打印的内容\n", new InnerResultCallbcak() {
                @Override
                public void onRunResult(boolean isSuccess) {
                    //返回接⼝执⾏的情况 成功/失败
                    Log.e("tag", "onRunResult="+isSuccess );
                }

                @Override
                public void onReturnString(String result) {
                    //部分接⼝会异步返回查询数据
                    Log.e("tag", "onReturnString="+result );
                }

                @Override
                public void onRaiseException(int code, String msg) throws RemoteException {
                    //接⼝执⾏失败时，返回的异常状态
                    Log.e("tag", "onRaiseException="+code );
                }

                @Override
                public void onPrintResult(int code, String msg) throws RemoteException {
                    //事务模式下的打印结果返回
                    Log.e("tag", "onPrintResult="+code );
                }
            });

        } catch (RemoteException r) {

        }
    }
}